{extend name="app/shop/view/base.html"/}
{block name="resources"}
<link rel="stylesheet" href="__STATIC__/ext/video/video.css">
<style>
	.layui-form-selected dl{z-index: 1000;}
	.upload-img-block .upload-img-box .upload-default{position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);}
	.multiple-uploading{display: flex;flex-wrap: wrap;}
	.multiple-uploading .multiple-item{position: relative;display: flex;justify-content: center;align-items: center;flex-direction: column;margin-bottom: 10px;padding: 10px;width: 100px;height: 100px;border: 1px dashed #ddd;text-align: center;box-sizing: border-box;line-height: 1;color: #5a5a5a;}
	.multiple-uploading .multiple-item p{line-height: 20px;margin-top: 5px;}
	.multiple-uploading .multiple-item span{display: none;position: absolute;top: -10px;right: -7px;width: 20px;height: 20px;border: 1px solid #999;color: #999;background-color: #fff;border-radius: 50%;line-height: 1;font-size: 18px;cursor: pointer;}
	.multiple-uploading .multiple-item:hover .icon{display: block;}
	.multiple-uploading .multiple-item ~ .multiple-item{margin-left: 10px;}
	.multiple-uploading .iconfont{font-size: 30px;color: #6D7278;}
	#multiple_uploading{cursor: pointer;}
	.multiple-item img{max-width: 100%;max-height: 100%;}

	/*上传视频相关*/
	.video-thumb{display:block;float:left;width:250px;height:120px;position:relative}
	.delete-video{width:51px;height:51px;cursor:pointer;position:absolute;right:150px;top:45px;color:#fff;z-index:100}
	.replace-video{width:75px;height:51px;cursor:pointer;position:absolute;right:50px;top:45px;color:#fff;z-index:100}
	.replace-video2{right:100px}
	.mask{position:absolute;left:0;width:250px;height:92px;background:#000;opacity:.6;cursor:pointer;z-index:10}
	.del-img{width:14px;margin-right:5px;padding-bottom:2px}
	.up-img{width:14px;height:14px;margin-right:5px;padding-bottom:3px}
	.up-video{position:absolute;left:0;width:250px;height:92px;cursor:pointer;z-index:10}
	.video-thumb .hide{display:none!important}
	.video-thumb>#goods_video{width:100%!important;height:121px;background:#fff}
	.hide{display:none}
</style>
{/block}
{block name="main"}
<div class="layui-collapse tips-wrap">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">操作提示</h2>
		<ul class="layui-colla-content layui-show">
			<li>管理员可以在此页编辑店铺笔记</li>
		</ul>
	</div>
</div>

<div class="layui-form form-wrap">

	<div class="layui-form-item">
		<label class="layui-form-label short-label"><span class="required">*</span>笔记标题：</label>
		<div class="layui-input-inline">
			<input type="text" name="note_title" value="{$info.note_title}" lay-verify="required" maxlength="40" autocomplete="off" placeholder="笔记标题最多40个字" class="layui-input len-long">
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label short-label">摘要：</label>
		<div class="layui-input-block">
			<textarea name="note_abstract" class="layui-textarea len-long" maxlength="100">{$info.note_abstract}</textarea>
		</div>
		<div class="word-aux">笔记摘要最多可输入100个字</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>封面：</label>
		<div class="layui-input-inline">
			<input type="radio" name="cover_type" value="0" title="单图" lay-filter="cover_type" {if $info.cover_type == 0} checked {/if}>
			<input type="radio" name="cover_type" value="1" title="多图" lay-filter="cover_type" {if $info.cover_type == 1} checked {/if}>
		</div>
	</div>

	<!-- 图片上传 -->
	<div class="layui-form-item">
		<label class="layui-form-label"></label>
		<div class="layui-input-block img-upload">
			<div class="upload-img-block square simple-uploading {if $info.cover_type != 0}layui-hide{/if}">
				<div class="upload-img-box" id="img">
					<div class="upload-default">
						<i class="iconfont iconshangchuan"></i>
						<p>点击上传</p>
					</div>
				</div>
			</div>

			<div class="multiple-uploading {if $info.cover_type != 1}layui-hide{/if}">
				<div class="multiple-item" id="multiple_uploading">
					<i class="iconfont iconshangchuan"></i>
					<p>点击上传</p>
				</div>
			</div>
		</div>
		<div class="word-aux">推荐使用 750x420 像素的图片 最多上传1张</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>笔记分组：</label>
		<div class="layui-input-inline">
			<select name="group_id" lay-verify="required">
				<option value="">请选择</option>
				{foreach $group_list as $v}
				<option value="{$v['group_id']}" {if $info.group_id == $v.group_id} selected {/if}>{$v['group_name']}</option>
				{/foreach}
			</select>
		</div>
	</div>

	<div class="layui-form-item goods_list">
		<label class="layui-form-label">关联商品：</label>

		<div class="layui-input-block">
			<table id="selected_goods_list"></table>
			<button class="layui-btn" onclick="addGoods()">选择商品</button>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>添加视频：</label>
		<div class="layui-input-block">
			<input type="hidden" name="video_path"  placeholder="在此输入外链视频地址" value="{$info.video_path}" autocomplete="off" class="layui-input len-long">
			<div class="video-thumb">
				<video id="goods_video" class="video-js vjs-big-play-centered" controls="" poster="SHOP_IMG/goods_video_preview.png" preload="auto"></video>
			</div>
			<div id="videoUpload2" class="up-video" title="商品视频" >
				<span class="delete-video hide" onclick="deleteVideo()"><img class="del-img" src="SHOP_IMG/delete.png">删除</span>
				<span id="" class="replace-video hide js-add-goods-video" ><img class="up-img" src="SHOP_IMG/upload.png">上传视频</span>
			</div>

		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">发布时间：</label>
		<div class="layui-input-inline">
			<input type="radio" name="is_show_release_time" value="1" title="显示" {if $info.is_show_release_time == 1} checked {/if}>
			<input type="radio" name="is_show_release_time" value="0" title="不显示" {if $info.is_show_release_time == 0} checked {/if}>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">阅读次数：</label>
		<div class="layui-input-inline">
			<input type="radio" name="is_show_read_num" value="1" title="显示" {if $info.is_show_read_num == 1} checked {/if}>
			<input type="radio" name="is_show_read_num" value="0" title="不显示" {if $info.is_show_read_num == 0} checked {/if}>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">点赞次数：</label>
		<div class="layui-input-inline">
			<input type="radio" name="is_show_dianzan_num" value="1" title="显示" {if $info.is_show_dianzan_num == 1} checked {/if}>
			<input type="radio" name="is_show_dianzan_num" value="0" title="不显示" {if $info.is_show_dianzan_num == 0} checked {/if}>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">虚拟阅读数：</label>
		<div class="layui-input-inline">
			<input type="number" min="0" name="initial_read_num" value="{$info.initial_read_num}" onchange="detectionNumType(this,'integral')" class="layui-input">
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">虚拟点赞数：</label>
		<div class="layui-input-inline">
			<input type="number" min="0" name="initial_dianzan_num" value="{$info.initial_dianzan_num}" onchange="detectionNumType(this,'integral')" class="layui-input">
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">排序：</label>
		<div class="layui-input-inline">
			<input type="number" min="0" name="sort" value="{$info.sort}" class="layui-input">
		</div>
	</div>

	<input type="hidden" name="note_id" value="{$info.note_id}"/>
	<input type="hidden" name="goods_ids" value="{$info.goods_ids}"/>
	<input type="hidden" name="note_type" value="{$info.note_type}"/>

	<div class="form-row">
		{if $info.status == 1}
		<button class="layui-btn" lay-submit lay-filter="save">保存</button>
		<button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
		{else /}
		<button class="layui-btn" lay-submit lay-filter="saveDrafts">保存</button>
		<button class="layui-btn" lay-submit lay-filter="save">立即发布</button>
		<button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
		{/if}
	</div>

</div>

<!-- 操作 -->
<script type="text/html" id="operation">
	<div class="table-btn">
		<a class="layui-btn" onclick="delGoods({{d.LAY_INDEX}})">删除</a>
	</div>
</script>

{/block}
{block name="script"}
	<script src="__STATIC__/ext/video/videojs-ie8.min.js"></script>
	<script src="__STATIC__/ext/video/video.min.js"></script>
<script>
var form,upload,
		IMAGE_MAX = 9, //最多可以上传多少张图片
		imageCollection = [], //图片集合
		selectedGoodsId = [],
		goods_id=[],
		repeat_flag = false,
		coverType = '{$info.cover_type}',
		coverImg = '{$info.cover_img}',
		goods_list = {:json_encode($info.goods_list, JSON_UNESCAPED_UNICODE)};

initImg();//初始化图片
function initImg(){
	if (coverType == 0){
		imageCollection.push(coverImg);
		//单图
		if (coverImg) $("#img").html("<img src=" + ns.img(coverImg) + " >");
	} else if (coverType == 1) {
		//多图
		var coverImgArr = coverImg.split(','),
				html = '';

		imageCollection = coverImgArr;
		for (var i = 0; i < coverImgArr.length; i++) {
			html += `<div class="multiple-item">`;
			html += `<img src="${ns.img(coverImgArr[i])}" alt="">`;
			html += `<span class="icon">x</span>`;
			html += `</div>`;
		}

		$(".multiple-uploading").append(html);
	}
}

loadVideo(true);

//初始化商品
$.each(goods_list, function(index, item) {
	var id = item.goods_id;
	selectedGoodsId.push(id);
	goods_id.push(id);
});
$("input[name='goods_ids']").val(goods_id.toString());

renderTable(goods_list); // 初始化表格

layui.use(['form'], function() {
	form = layui.form;
	form.render();

	/**
	 * 图片切换
	 */
	form.on('radio(cover_type)', function(data){
		//0单图  1多图
		if (data.value == 0){

			if(imageCollection.length) {
				imageCollection.splice(1, imageCollection.length);
				var val = '<img src="' + ns.img(imageCollection[0]) + '" alt="">';
				$("#img").html(val);
			}

			$(".simple-uploading").removeClass("layui-hide");
			$(".multiple-uploading").addClass("layui-hide");

			$(".simple-uploading").parents(".layui-form-item").find('.word-aux').text('推荐使用 750x420 像素的图片 最多上传1张');

		} else{
			if(imageCollection.length) {
				var html = '';
				html += `<div class="multiple-item" id="multiple_uploading">`;
				html += `<i class="iconfont iconshangchuan"></i>`;
				html += `<p>点击上传</p>`;
				html += `</div>`;

				for (var i = 0; i < imageCollection.length; i++) {
					html += `<div class="multiple-item">`;
					html += `<img src="${ns.img(imageCollection[i])}" alt="">`;
					html += `<span class="icon">x</span>`;
					html += `</div>`;
				}

				$(".multiple-uploading").html(html);
			}

			$(".simple-uploading").addClass("layui-hide");
			$(".multiple-uploading").removeClass("layui-hide");

			$(".simple-uploading").parents(".layui-form-item").find('.word-aux').text('推荐使用 350x350 像素的图片，最多上传9张');
		}
	});

	// 单图上传
	$("body").on("click", "#img", function () {
		openAlbum(function (data) {
			imageCollection = [];
			imageCollection.push(data[0].pic_path);
			imageCollection.splice(1, imageCollection.length);
			var val = '<img src="' + ns.img(imageCollection[0]) + '" alt="">';
			$("#img").html(val);
		}, 1);
	});

	//多图上传
	$("body").on("click","#multiple_uploading", function () {
		var html = '';
		openAlbum(function (data) {
			for (var i = 0; i < data.length; i++) {
				if (imageCollection.length < IMAGE_MAX){
					imageCollection.push(data[i].pic_path);

					html += `<div class="multiple-item">`;
					html += `<img src="${ns.img(data[i].pic_path)}" alt="">`;
					html += `<span class="icon">x</span>`;
					html += `</div>`;
				}
			}
			if (imageCollection.length >= IMAGE_MAX)
				$("#multiple_uploading").addClass("layui-hide");

			$(".multiple-uploading").append(html);

		}, IMAGE_MAX);
	});

	$("body").on('click', '.multiple-item .icon', function () {
		var index = $(this).parent().index() - 1;
		imageCollection.splice(index,1);
		$(this).parent().remove();

		if (imageCollection.length < IMAGE_MAX)
			$("#multiple_uploading").removeClass("layui-hide");
	});

	/**
	 * 表单提交(立即发布)
	 */
	form.on('submit(save)', function(data){
		var field = data.field;
		field.status = 1;
		formSubmit(field)
	});

	/**
	 * 表单提交(草稿箱)
	 */
	form.on('submit(saveDrafts)', function(data){
		var field = data.field;
		field.status = 0;
		formSubmit(field)
	});

});

/**
 *  提交
 */
function formSubmit(data)
{
	if (!imageCollection.length){
		layer.msg('请选择封面图！', {icon: 5, anim: 6});
		return;
	}

	// if (selectedGoodsId.length == 0) {
	//     layer.msg('请选择参与活动的商品！', {icon: 5, anim: 6});
	//     return;
	// }

	data.cover_img = imageCollection.join();
	if(data.video_path == ''){
		layer.msg('请选择视频！', {icon: 5, anim: 6});
		return;
	}

	if(data.sort < 0){
		layer.msg("排序号不能小于0");
		return false;
	}

	if(repeat_flag) return;
	repeat_flag = true;

	$.ajax({
		type: 'POST',
		dataType: 'JSON',
		url: ns.url("notes://shop/notes/edit"),
		data: data,
		async: false,
		success: function(res){
			repeat_flag = false;

			if (res.code == 0) {
				layer.confirm('编辑成功', {
					title:'操作提示',
					btn: ['返回列表', '继续编辑'],
					yes: function(){

						if(data.status == 0){
							location.href = ns.url("notes://shop/notes/drafts");
						}else{
							location.href = ns.url("notes://shop/notes/lists");
						}
					},
					btn2: function() {
						location.reload();
					}
				});
			}else{
				layer.msg(res.message);
			}
		}
	})
}

// 表格渲染
function renderTable(goods_list) {
	//展示已知数据
	table = new Table({
		elem: '#selected_goods_list',
		cols: [
			[{
				field: 'goods_name',
				title: '商品名称',
				unresize: 'false',
				width: '50%'
			}, {
				field: 'price',
				title: '商品价格(元)',
				unresize: 'false',
				align: 'right',
				width: '20%',
				templet: function(data) {
					return '￥' + data.price;
				}
			}, {
				field: 'goods_stock',
				title: '库存',
				unresize: 'false',
				align: 'center',
				width: '20%'
			}, {
				title: '操作',
				toolbar: '#operation',
				unresize: 'false',
				align:'right'
			}],
		],
		data: goods_list,
	});
}

// 添加商品
function addGoods(){
	goodsSelect(function (res) {
		if (!res.length) return false;
		goods_id = [];
		goods_list = [];
		for(var i=0;i<res.length;i++) {
			goods_id.push(res[i].goods_id);
			goods_list.push(res[i]);
		}

		renderTable(goods_list);
		$("input[name='goods_ids']").val(goods_id.toString());
		selectedGoodsId = goods_id;

	},selectedGoodsId, {mode: "spu"});
}

// 删除选中商品
function delGoods(index) {
	goods_list.splice(index, 1);
	renderTable(goods_list);

	selectedGoodsId.splice(index, 1);
	goods_id = selectedGoodsId;
	$("input[name='goods_ids']").val(goods_id.toString());
}

function back() {
	location.href = ns.url("notes://shop/notes/drafts");
}

//检测数据类型
function detectionNumType(el, type) {
	var value = $(el).val();
	//大于零 且 不是小数
	if (value < 0 && type == 'integral')
		$(el).val(0);
	else if (type == 'integral')
		$(el).val(Math.round(value));

	//大于1 且 不是小数
	if (value < 1 && type == 'positiveInteger')
		$(el).val(1);
	else if (type == 'positiveInteger')
		$(el).val(Math.round(value));
	//大于零可以是小数
	if (type == 'positiveNumber') {
		value = parseFloat(value).toFixed(2);
		if (value < 0)
			$(el).val(0);
		else
			$(el).val(value);
	}
}
function deleteVideoClass(){
	$('#goods_video').removeClass("vjs-error");
}
//删除已选择的视频
function deleteVideo() {
	var src = $("input[name='video_path']").val();
	if (src != "") {
		var video = 'goods_video';
		var myPlayer = videojs(video);
		videojs(video).ready(function () {
			var myPlayer = this;
			myPlayer.pause();
		});

		$("#goods_video_html5_api").attr('src', "");
		$(".vjs-modal-dialog-content").hide();
		$(".vjs-error-display").hide();
		setTimeout("deleteVideoClass()",30 );
		$('#goods_video_html5_api').attr('controls', true);
		$(".vjs-poster").hide();

		$("input[name='video_path']").val('');
	}
}

$('body').on('mouseover', '#videoUpload2', function () {
	$(this).addClass('mask');
	var src = $('#goods_video_html5_api').attr('src');
	if(src) {
		$(".delete-video").removeClass('hide');
		$(".replace-video").removeClass('hide').removeClass('replace-video2');
	} else {
		$(".replace-video").removeClass('hide').addClass('replace-video2');
	}

});

$('body').on('mouseout', '#videoUpload2', function () {
	$(this).removeClass('mask');
	$(".delete-video").addClass('hide');
	$(".replace-video").addClass('hide');
});
//添加商品视频
$("body").on("click", ".js-add-goods-video", function () {
	openAlbum(function (data) {
		if(data.length > 0) {
			$("input[name='video_path']").val(data[0]['pic_path']);
			loadVideo();
		}
	}, 1, 0, 'video');
});
/**
 * 加载视频
 * @param flag 是否暂停
 */
function loadVideo(flag) {

	var video_path = $("input[name='video_path']").val();
	if (!video_path.length) return;

	var video = "goods_video";
	var myPlayer = videojs(video);
	var value = ns.img(video_path);

	videojs(video).ready(function () {
		var myPlayer = this;
		myPlayer.src(value);
		myPlayer.load(value);
		myPlayer.play();
		// if (flag) {
		// 	setTimeout(function () {
		// 		myPlayer.pause();
		// 	}, 10);
		// }
		setTimeout(function () {
			if (!$(".video-thumb .vjs-error-display").hasClass("vjs-hidden")) {
				$("input[name='video_path']").val("");//video.js Line:7873
				layer.msg("媒体不能加载，要么是因为服务器或网络失败，要么是因为格式不受支持。");
			} else {
			}
		}, 1000);
	});
}
</script>
{/block}